/*
 * @Author: your name
 * @Date: 2021-10-13 14:54:41
 * @LastEditTime: 2021-10-15 13:52:18
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /egg_dome/app/controller/login.js
 */

//  200 === 成功
//  301 === 没有登陆

'use strict';

const Controller = require('egg').Controller;

// TC 是管理员
// USER 是 游客
const userList = [
    {
        username: '123',
        password: '123',
        author: 'USER'
    },
    {
        username: '1902A',
        password: '1902A',
        author: 'TC'
    }
]

class LoginController extends Controller {
  async userLogin() {
    const { ctx } = this;
    let {username, password} = ctx.request.body;
    // find 返回的是 一个对象 filter返回一个数组 数组内就是成立的数据
    const user = userList.find(item => item.username === username && item.password === password );
    if(user) {
        const token = this.app.jwt.sign(user, this.app.config.jwt.secret);
        ctx.body = {
            code: 200,
            user,
            token,
            msg: '登陆成功'
        }
    } else {
        ctx.body = {
            code: 200,
            msg: '登陆失败'
        }
    }
  }
}

module.exports = LoginController;